package modelLayer;

import java.util.ArrayList;
import modelLayer.*;
import modelLayer.Exceptions.*;

public class Accounting {
    private ArrayList<Account> accounts;
    private static Accounting instance = null;
    
    public Accounting() {
        accounts= new ArrayList<>();
    }
    
    public static Accounting getInstance() {
        if (instance == null) {
            instance= new Accounting();
        }
        return instance;
    }
   
    public void addAccount(Account account) throws DuplicateAccountNameException {
        for (Account acc : accounts) {
            if ( account.getName().equals(acc.getName())) {
                throw new DuplicateAccountNameException("The account already exists!", account.getName(), account.getBalance());
                }
            }
        accounts.add(account);
    }
    
    public Account findByName(String name){
        for(Account acc: accounts) {
            if(acc.getName().equals(name)) 
                return acc;
        }
        return null;
    }
    public void showInfo(String name){
         for(Account acc: accounts) {
            if(acc.getName().equals(name)) {
                //System.out.println("the name of the account is "+acc.getName());
                System.out.println("the balance of the account is "+acc.getBalance());
            }
        }
        
    }
    
}
    
